package com.fieldchain.ex.fa100.ui.money.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.bean.SimpleBean;
import com.fieldchain.ex.fa100.bean.event.CoinEvent;
import com.fieldchain.ex.fa100.utils.common.RQCode;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.BeanRequestTwo;
import com.fieldchain.ex.fa100.utils.screen.MyToast;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/10/10
 */
public class RechargeActivity extends MyBaseActivity implements View.OnClickListener {
    private RelativeLayout reaBack;
    private LinearLayout linTop;
    private TextView tvCoin;
    private TextView tvRecode;
    private ImageView ivImgcode;
    private TextView tvAddress;
    private RelativeLayout reaCopy;
    private TextView tvNotice;
    private String coin = "";
    private String coinId = "";
    private String notice = "";

    private String address = "";

    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        linTop = findViewById(R.id.lin_top);
        tvCoin = findViewById(R.id.tv_coin);
        tvRecode = findViewById(R.id.tv_recode);
        ivImgcode = findViewById(R.id.iv_imgcode);
        tvAddress = findViewById(R.id.tv_address);
        reaCopy = findViewById(R.id.rea_copy);
        tvNotice = findViewById(R.id.tv_notice);
    }

    @Override
    public void setValue() {
        coin = getIntent().getStringExtra("coin").toUpperCase();
        coinId = getIntent().getStringExtra("coinId");
        if (coin != null && coin.length() > 0) {
            tvCoin.setText(coin);
        }

        reaBack.setOnClickListener(this);
        tvRecode.setOnClickListener(this);
        linTop.setOnClickListener(this);
        reaCopy.setOnClickListener(this);


        notice = "充值须知\n1. 禁止向BTC地址充值除BTC之外的资产，任何充入BTC地址之外的非BTC资产将不可找回；\n2. BTC地址充值需要1个网络确认才能到账。";
        notice = notice.replace("BTC", coin.toUpperCase());
        tvNotice.setText(notice);

        getAddress();
    }

    @Override
    public int getLayout() {
        return R.layout.activity_recharge;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rea_back:
                finish();
                break;
            case R.id.lin_top:
                startActivity(new Intent(mContext, RecChoseActivity.class));
                break;
            case R.id.rea_copy:
                copy(tvAddress);
                break;
            case R.id.tv_recode:
                Intent intent = new Intent(mContext, RecListActivity.class);
                intent.putExtra("coinId", coinId);
                intent.putExtra("type", 1);
                startActivity(intent);
                break;

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void rechargeEvent(CoinEvent et) {
        coinId = et.getCoinId() + "";
        tvCoin.setText(et.getCoin().toUpperCase());
        notice = notice.replace(coin.toUpperCase(), et.getCoin().toUpperCase());
        coin = et.getCoin();
        tvNotice.setText(notice);
        getAddress();

    }

    public void copy(TextView tv) {
        if(tv.getText().toString().length()==0){
            MyToast.show("暂无钱包地址");
            return;
        }
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        ClipData clipData = ClipData.newPlainText("", tv.getText().toString());
        cm.setPrimaryClip(clipData);
        MyToast.show(this, "复制成功");
    }

    public void getAddress() {
        HashMap map = new HashMap();
        map.put("coinId", coinId);
        String url = Constant.address + coinId;

        HttpRequest.getInstance().getRequest(map, url, new BeanRequestTwo<SimpleBean>() {
            @Override
            public void onSucess(SimpleBean simpleBean, int code, String msg) {
                if (code == 200) {
                    address = simpleBean.getData();
                    Log.e("address",address);
                    tvAddress.setText(address);
                    ivImgcode.setImageBitmap(RQCode.encodeAsBitmap(address));
                }
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }
}
